<!-- 代理商详情页面 -->
<template>
  <s-layout title="代理商详情" :bgStyle="{ color: '#FFF' }">
    <view class="detail-container" v-if="state.agent">
      <!-- 基本信息 -->
      <view class="detail-section">
        <view class="section-header">
          <view class="section-title">基本信息</view>
          <view class="agent-status" :class="{ 'active': state.agent.status, 'inactive': !state.agent.status }">
            {{ state.agent.status ? '启用' : '禁用' }}
          </view>
        </view>
        <view class="detail-content">
          <view class="detail-item">
            <view class="item-label">代理商ID</view>
            <view class="item-value">{{ state.agent.id }}</view>
          </view>
          <view class="detail-item">
            <view class="item-label">用户名称</view>
            <view class="item-value">{{ state.agent.userName }}</view>
          </view>
          <view class="detail-item">
            <view class="item-label">用户类型</view>
            <view class="item-value">{{ getUserTypeName(state.agent.userType) }}</view>
          </view>
          <view class="detail-item">
            <view class="item-label">手机号</view>
            <view class="item-value">
              <text selectable>{{ state.agent.mobile }}</text>
              <button class="copy-btn" size="mini" @click="copyText(state.agent.mobile)">复制</button>
            </view>
          </view>
          <view class="detail-item" v-if="state.agent.agentLevel">
            <view class="item-label">代理商等级</view>
            <view class="item-value">{{ state.agent.agentLevel }}</view>
          </view>
          <view class="detail-item" v-if="state.agent.address">
            <view class="item-label">联系地址</view>
            <view class="item-value">{{ state.agent.address }}</view>
          </view>
        </view>
      </view>

      <!-- 关联信息 -->
      <view class="detail-section" v-if="hasAssociationInfo">
        <view class="section-title">关联信息</view>
        <view class="detail-content">
          <view class="detail-item" v-if="state.agent.memberId">
            <view class="item-label">关联会员ID</view>
            <view class="item-value">{{ state.agent.memberId }}</view>
          </view>
          <view class="detail-item" v-if="state.agent.adminId">
            <view class="item-label">管理员ID</view>
            <view class="item-value">{{ state.agent.adminId }}</view>
          </view>
          <view class="detail-item" v-if="state.agent.assigned">
            <view class="item-label">指派生产方</view>
            <view class="item-value">{{ state.agent.assigned }}</view>
          </view>
        </view>
      </view>

      <!-- 操作信息 -->
      <view class="detail-section" v-if="hasOperationInfo">
        <view class="section-title">操作信息</view>
        <view class="detail-content">
          <view class="detail-item" v-if="state.agent.creator">
            <view class="item-label">创建人</view>
            <view class="item-value">{{ state.agent.creator }}</view>
          </view>
          <view class="detail-item" v-if="state.agent.creatorName">
            <view class="item-label">创建者姓名</view>
            <view class="item-value">{{ state.agent.creatorName }}</view>
          </view>
          <view class="detail-item" v-if="state.agent.updaterName">
            <view class="item-label">更新者姓名</view>
            <view class="item-value">{{ state.agent.updaterName }}</view>
          </view>
          <view class="detail-item" v-if="state.agent.createTime">
            <view class="item-label">创建时间</view>
            <view class="item-value">{{ formatTime(state.agent.createTime) }}</view>
          </view>
          <view class="detail-item" v-if="state.agent.updateTime">
            <view class="item-label">更新时间</view>
            <view class="item-value">{{ formatTime(state.agent.updateTime) }}</view>
          </view>
        </view>
      </view>

      <!-- 备注信息 -->
      <view class="detail-section" v-if="state.agent.remark">
        <view class="section-title">备注信息</view>
        <view class="detail-content">
          <view class="remark-content">{{ state.agent.remark }}</view>
        </view>
      </view>

      <!-- 操作按钮 -->
      <view class="action-section">
        <button class="action-btn edit-btn" @click="onEdit">编辑代理商</button>
        <button class="action-btn delete-btn" @click="onDelete">删除代理商</button>
      </view>
    </view>

    <!-- 加载状态 -->
    <view v-else-if="state.loading" class="loading-container">
      <text>加载中...</text>
    </view>

    <!-- 错误状态 -->
    <view v-else class="error-container">
      <text>加载失败，请重试</text>
      <button class="retry-btn" @click="loadAgentDetail">重新加载</button>
    </view>
  </s-layout>
</template>

<script setup>
  import { onLoad } from '@dcloudio/uni-app';
  import { reactive, computed } from 'vue';
  import AgentApi from '@/sheep/api/agent/agent';
  import { formatDate } from '@/sheep/helper/utils';

  const state = reactive({
    agent: null,
    loading: true,
    agentId: null
  });

  // 用户类型映射
  const userTypeMap = {
    1: '企业',
    2: '个人',
    3: '其他'
  };

  // 是否有关联信息
  const hasAssociationInfo = computed(() => {
    return state.agent && (state.agent.memberId || state.agent.adminId || state.agent.assigned);
  });

  // 是否有操作信息
  const hasOperationInfo = computed(() => {
    return state.agent && (
      state.agent.creator || 
      state.agent.creatorName || 
      state.agent.updaterName || 
      state.agent.createTime || 
      state.agent.updateTime
    );
  });

  // 获取用户类型名称
  function getUserTypeName(userType) {
    return userTypeMap[userType] || '未知';
  }

  // 格式化时间
  function formatTime(time) {
    return formatDate(time, 'yyyy-mm-dd hh:MM:ss');
  }

  // 复制文本
  function copyText(text) {
    uni.setClipboardData({
      data: text,
      success: () => {
        uni.showToast({
          title: '复制成功',
          icon: 'success'
        });
      },
      fail: () => {
        uni.showToast({
          title: '复制失败',
          icon: 'error'
        });
      }
    });
  }

  // 加载代理商详情
  async function loadAgentDetail() {
    if (!state.agentId) return;
    
    state.loading = true;
    try {
      let { code, data } = await AgentApi.getAgent(state.agentId);
      if (code === 0) {
        state.agent = data;
      } else {
        uni.showToast({
          title: '获取代理商信息失败',
          icon: 'error'
        });
      }
    } catch (error) {
      uni.showToast({
        title: '网络错误，请重试',
        icon: 'error'
      });
    } finally {
      state.loading = false;
    }
  }

  // 编辑代理商
  function onEdit() {
    uni.navigateTo({
      url: `/pages/agent-form/index?id=${state.agentId}`
    });
  }

  // 删除代理商
  function onDelete() {
    uni.showModal({
      title: '确认删除',
      content: '确定要删除这个代理商吗？删除后无法恢复。',
      success: async (res) => {
        if (res.confirm) {
          try {
            let { code, msg } = await AgentApi.deleteAgent(state.agentId);
            if (code === 0) {
              uni.showToast({
                title: '删除成功',
                icon: 'success'
              });
              setTimeout(() => {
                uni.navigateBack();
              }, 1500);
            } else {
              uni.showToast({
                title: msg || '删除失败',
                icon: 'error'
              });
            }
          } catch (error) {
            uni.showToast({
              title: '删除失败',
              icon: 'error'
            });
          }
        }
      }
    });
  }

  // 页面加载
  onLoad((options) => {
    if (options.id) {
      state.agentId = options.id;
      loadAgentDetail();
    } else {
      uni.showToast({
        title: '参数错误',
        icon: 'error'
      });
      setTimeout(() => {
        uni.navigateBack();
      }, 1500);
    }
  });
</script>

<style lang="scss" scoped>
.detail-container {
  padding: 20rpx;
}

.detail-section {
  background: #fff;
  border-radius: 16rpx;
  margin-bottom: 20rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx;
  background: #f8f9fa;
  border-bottom: 2rpx solid #e9ecef;
}

.section-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #333;
}

.agent-status {
  padding: 8rpx 16rpx;
  border-radius: 20rpx;
  font-size: 24rpx;
  
  &.active {
    background: #e8f5e8;
    color: #52c41a;
  }
  
  &.inactive {
    background: #fff2f0;
    color: #ff4d4f;
  }
}

.detail-content {
  padding: 30rpx;
}

.detail-item {
  display: flex;
  margin-bottom: 30rpx;
  
  &:last-child {
    margin-bottom: 0;
  }
}

.item-label {
  width: 200rpx;
  font-size: 28rpx;
  color: #666;
  flex-shrink: 0;
}

.item-value {
  flex: 1;
  font-size: 28rpx;
  color: #333;
  display: flex;
  align-items: center;
  
  .copy-btn {
    margin-left: 20rpx;
    background: #007aff;
    color: #fff;
    border: none;
    border-radius: 4rpx;
    font-size: 20rpx;
    padding: 8rpx 16rpx;
  }
}

.remark-content {
  font-size: 28rpx;
  color: #333;
  line-height: 1.6;
  padding: 20rpx;
  background: #f8f9fa;
  border-radius: 8rpx;
}

.action-section {
  display: flex;
  gap: 20rpx;
  padding: 40rpx 0;
}

.action-btn {
  flex: 1;
  height: 88rpx;
  border-radius: 8rpx;
  font-size: 32rpx;
  font-weight: 600;
  border: none;
  
  &.edit-btn {
    background: #007aff;
    color: #fff;
  }
  
  &.delete-btn {
    background: #ff4d4f;
    color: #fff;
  }
}

.loading-container, .error-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 100rpx 20rpx;
  
  text {
    font-size: 28rpx;
    color: #666;
    margin-bottom: 40rpx;
  }
  
  .retry-btn {
    background: #007aff;
    color: #fff;
    border: none;
    border-radius: 8rpx;
    padding: 20rpx 40rpx;
    font-size: 28rpx;
  }
}
</style>

